var loginPage = angular.module('loginPage',[]);
var theStatus = false;
function bgCheck() {    
    if(theStatus){
        $('.remember i')[0].className = "noSelect";
    }else{
    	$('.remember i')[0].className= "isSelect";
    }
    theStatus = !theStatus;
};  

loginPage.controller('myCtrl',function($scope,$http) {
    // 双向控制 修改ng-model内容这里的numbers也会变
    $scope.username = "";
    $scope.passwords = "";
    $scope.selectCom = "";
    $scope.verify = "";


    // 选择框 选项内容获取之后放入即可，自动循环列入
    $scope.coms = ["Google", "Runoob", "Taobao"];
    
    
    //通过$http取到验证码 然后对验证码处理显示
    $scope.imgSrc =[];
    yanzheng();
    $scope.print = function () {
        //模拟数据
        yanzheng();
       
    };
    function yanzheng() {
        verifyN = parseInt((Math.random()*9)+1)*1000 + parseInt(Math.random()*10)*100 + parseInt(Math.random()*10)*10 + parseInt(Math.random()*10) + "";      
        for (var i = 0; i < 4; i++) {
             $scope.imgSrc[i] = "img/"+ verifyN.charAt(i) + ".png";
        }
   }

    $scope.meths = function () {
        if ($("select")[0].selectedIndex == 0) {
            $("select").css("color","#999");
        }else{
            $("select").css("color","#333");
        }   
    }
    
    // 第一次验证
    $scope.usertest = function () {
        if($scope.username == ""){
            $('.numTest').css('display','block');
            setTimeout(function() {
                $('.numTest').css('display','none');
            }, 3000);
            return false;
        };
        if($scope.passwords == ""){
            $('.passwTest').css('display','block');
            setTimeout(function() {
                $('.passwTest').css('display','none');
            }, 3000);
            return false;
        }
        if($scope.selectCom == ""){
            $('.seleTest').css('display','block');
            setTimeout(function() {
                $('.seleTest').css('display','none');
            }, 3000);
            return false;
        }
        if($scope.verify != verifyN){
            $('.verifyTest').css('display','block');
            setTimeout(function() {
                $('.verifyTest').css('display','none');
            }, 3000);
            return false;
        }
        LOGIN.doLogin();
    }
    
//    var redirectUrl = "${redirect}";
    var LOGIN = {
    		doLogin:function() {
    			$.post("doLogin.html", $("#formlogin").serialize(),function(data){
    				/* alert(data); */
    				if (data.status == 200) {
    					location.href = "index.html";
    				} else {
//    					alert("登录失败，原因是：" + data.msg);
    					$(".formError").css("display","block")
    					$("#loginname").select();
    				}
    			},"json");
    		}
    };
    
});


var vw = document.documentElement.clientWidth;
var vh = document.documentElement.clientHeight;
var bgPosition = document.getElementById("bg-position"); 
// 940 460

bgPosition.style.top = (vh - 460)/2 + "px";
bgPosition.style.right = (vw - 940)/2 + "px";

window.onresize = function () {
    vw = document.documentElement.clientWidth;
    vh = document.documentElement.clientHeight;
    
    bgPosition.style.top = (vh - 460)/2 + "px";
    bgPosition.style.right = (vw - 940)/2 + "px";
}



/////原有js



$(function() {
	// Waves初始化
	Waves.displayEffect();
	// 输入框获取焦点后出现下划线
	$('.form-control').focus(function() {
		$(this).parent().addClass('fg-toggled');
	}).blur(function() {
		$(this).parent().removeClass('fg-toggled');
	});
});
//Checkbix.init();
$(function() {
	// 点击登录按钮
	$('#login-bt').click(function() {
		doExeLogin();
	});
	// 回车事件
	$('#username, #password,#verify').keypress(function (event) {
		if (13 == event.keyCode) {
			doExeLogin();
		}
	});

	$("#rememberMe").change(function() { 
		if($(this).val() == 'true'){
			$(this).val('false')
			console.log($(this).val())
		}else{
			
			$(this).val('true')
			console.log($(this).val())
		}
	})		
});
//refresh 验证码
function getVCode(vCode){
		vCode.src=$("#webroot").val()+"/getVCode?date"+new Date();			
}

// 登录
function doExeLogin() {
	var userName=$("#username").val();
	var password=$("#password").val();
	var verifyCode = $('#verify').val();
	//var rememberMe = $('#rememberMe').val();
	if(userName==null||userName==''){
		$("#error").html("用户名不能为空！");
	}
	if(password==null||password==''){
		$("#error").html("密码不能为空！");
	}
	if(verifyCode==null||verifyCode==''||verifyCode.length!=4){
		$("#error").html("验证码格式不正确！");
	}
	if(userName!=null&&userName!=""&&password!=null&&password!=""&&verifyCode!=null&&verifyCode!=""&&verifyCode.length==4){
		$.ajax({
			url:$("#webroot").val()+"/exeLogin",
			data:{userName:userName,password:password,verifyCode:verifyCode,rememberMe:theStatus},
			dataType:"json",
			type:"POST",
			success:function (data) {
				if(data.result!=null&&data.result!=''){
					if(data.result=='000'){
						window.top.location.href=$("#webroot").val()+"/welcome";
					}else{
						$("#error").html(data.msg+" 错误代码："+data.result);
						$("#verifyNum").attr("src",$("#webroot").val()+"/getVCode?date"+new Date());
                    }
				}
			},error:function () {
				$("#error").html("系统繁忙，请稍后再试！");				
			}
		})
	}
}
